package org.xteam.box2text.old;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public abstract class Operator extends Box {
	
	protected List<Box> arguments;
	protected Map<String, Integer> options;
	
	public Operator(Box... boxes) {
		this(Arrays.asList(boxes));
	}
	
	public Operator(List<Box> boxes) {
		arguments = boxes;
		options = new HashMap<String, Integer>();
	}
	
	public Operator(Map<String, Integer> options, List<Box> boxes) {
		this.options = options;
		this.arguments = boxes;
	}

	public int size() {
		return arguments.size();
	}
	
	public Map<String, Integer> options() {
		return options;
	}
	
	protected List<Box> normalizeChildren(boolean splice) {
		List<Box> results = new ArrayList<Box>();
		for (Box box : arguments) {
			results.addAll(box.normalizeGroups(splice));
		}
		return results;
	}
	
	protected static List<Box> removeVertical(List<Box> boxes, boolean removeRow) {
		List<Box> results = new ArrayList<Box>();
		for (Box box : boxes) {
			results.add(box.removeVertical(removeRow));
		}
		return results;
	}
	
	public boolean isEmpty() {
		for (Box arg : arguments) {
			if (! arg.isEmpty())
				return false;
		}
		return true;
	}
	
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(getClass().getSimpleName());
		buffer.append("[");
		boolean isFirst = true;
		for (Box box : arguments) {
			if (isFirst)
				isFirst = false;
			else
				buffer.append(" ");
			buffer.append(box);
		}
		buffer.append("]");
		return buffer.toString();
	}
	
}
